<html lang="zh-CN">
<head>
  <meta charset="utf-8">
  <title>接口测试</title>
  <link href="css/bootstrap.min.css" rel="stylesheet">
  <link href="css/font-awesome.min.css" rel="stylesheet">
  <link href="css/site.min.css?v3" rel="stylesheet">
  <link href="favicon.jpg" rel="shortcut icon">
  <script src="js/jquery.min.js"></script>
</head>
<body>
  <?php
header('Content-type:text/html;charset=utf-8');

function fly_curl($url, $postFields = null) {
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.1)');
	curl_setopt($ch, CURLOPT_HEADER, 0);
	curl_setopt($ch, CURLOPT_FAILONERROR, 0);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	if (is_array($postFields) && 0 < count($postFields)) {
		$postBodyString = "";
		$postMultipart = false;
		foreach ($postFields as $k => $v) {
			if ("@" != substr($v, 0, 1)) {
				//判断是不是文件上传1
				$postBodyString .= "$k=" . urlencode($v) . "&";
			} else {
				//文件上传用multipart/form-data，否则用www-form-urlencoded
				$postMultipart = true;
			}
		}
		unset($k, $v);
		curl_setopt($ch, CURLOPT_POST, false);
		if ($postMultipart) {
			curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
		} else {
			curl_setopt($ch, CURLOPT_POSTFIELDS, substr($postBodyString, 0, -1));
		}
	}

	$reponse = curl_exec($ch);

	if (curl_errno($ch)) {
		throw new Exception(curl_error($ch), 0);
	} else {
		$httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
		if (200 !== $httpStatusCode) {
			throw new Exception($reponse, $httpStatusCode);
		}
	}

	curl_close($ch);
	return $reponse;
}

//https请求（支持GET和POST）
function https_request($url, $data = null, $noprocess = false) {
	$curl = curl_init();
	curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
	curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.3; WOW64; rv:39.0) Gecko/20100101 Firefox/39.0');
	$header = ['Accept-Charset: utf-8'];
	curl_setopt($curl, CURLOPT_URL, $url);
	curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
	curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
	//curl_setopt($curl, CURLOPT_SSLVERSION, 3);
	curl_setopt($curl, CURLOPT_HTTPHEADER, $header); /***$header 必须是一个数组***/
	curl_setopt($curl, CURLOPT_HEADER, FALSE);
	curl_setopt($curl, CURLINFO_HEADER_OUT, true);
	if (!empty($data)) {
		curl_setopt($curl, CURLOPT_POST, 1);
		curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
	}
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
	$output = curl_exec($curl);
	if ($noprocess) {
		return $output;
	}

	$errorno = curl_errno($curl);
	if ($errorno) {
		return ['curl' => false, 'errorno' => $errorno];
	} else {
		$res = json_decode($output, 1);
		/*if ($res['errcode']) {
			return ['errcode' => $res['errcode'], 'errmsg' => $res['errmsg']];
		} else {*/
		return $res;
		//}
	}
	curl_close($curl);
}

function wxHttpsRequest($url, $data = null) {
	$curl = curl_init();
	curl_setopt($curl, CURLOPT_URL, $url);
	curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
	curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
	if (!empty($data)) {
		curl_setopt($curl, CURLOPT_POST, 1);
		curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
	}
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
	$output = curl_exec($curl);
	$errorno = curl_errno($curl);
	curl_close($curl);
	if ($errorno) {
		return ['curl' => false, 'errorno' => $errorno];
	} else {
		$res = json_decode($output, 1);
		if ($res['errcode']) {
			return ['errcode' => $res['errcode'], 'errmsg' => $res['errmsg']];
		} else {
			return $res;
		}
	}
}

function microtime_float() {
	list($usec, $sec) = explode(" ", microtime());
	return ((float) $usec + (float) $sec);
}

$start = $end = 0;
if (isset($_POST['submit'])) {
	if (strstr($_POST['url'], '?')) {
		$url = sprintf("%s%s", $_POST['url'], '');
	} else {
		$url = sprintf("%s?%s", $_POST['url'], '');
	}
	$param = array();

	if (isset($_POST['param'])) {
		foreach ($_POST['param'] as $k => $item) {
			if (!empty($item['method']) && !empty($item['value'])) {
				if ($item['method'] == 'get') {
					$param[$item['method']][$item['name']] = $item['value'];
				} else {
					$param[$item['method']] = $item['value'];
				}
			}
		}
	}
	//print_r($param);exit;
	if (isset($param['get']) && !empty($param['get'])) {
		foreach ($param['get'] as $name => $value) {
			$url = sprintf("%s&%s=%s", $url, $name, $value);
		}
	}

	$post_data = null;
	if (isset($param['post']) && !empty($param['post'])) {
		$post_data = $param['post'];
	}

	$start = microtime_float();

	if ($post_data) {
		$return = wxHttpsRequest($url, $post_data);
	} else {
		$return = https_request($url);
	}

	$content = $return; //json_decode(urldecode($return), TRUE);
	if (!$content) {
		$content = $return;
	}
	$end = microtime_float();
}
?>


<div class="container">
  <div class="row row-offcanvas row-offcanvas-right">
    <div class="col-xs-12 col-sm-12">
      <div class="row" >
        <div class="col-xs-1 col-lg-4">
          <h1>接口测试</h1>
          <div class="thumbnail">
            <form class="form-signin" action="" method="post">
              <b>请填URL</b>:
              <textarea value="<?php echo isset($_POST['url']) ? $_POST['url'] : '1'; ?>" class="form-control" placeholder="填写完整地址，以http://或https://开头" type="text" name="url" required></textarea><br>
              <?php if (isset($_POST['param']) && !empty($_POST['param'])): ?>
                <?php foreach ($_POST['param'] as $k => $item): ?>
                  <?php if (!empty($item['method']) && !empty($item['name'])): ?>
                    <div class="thumbnail">
                      <b>参数name</b>:
                      <input style="width: 200px;height: 25px;" value="<?php echo $item['name']; ?>" placeholder="请填写" type="text" name="param[<?php echo $k; ?>][name]"><br>
                      <b>参数value</b>:
                      <textarea style="width:330px;height: 200px;" value="<?php echo $item['value']; ?>" placeholder="请填写" type="text" name="param[<?php echo $k; ?>][value]"></textarea><br>
                      <b>请求方式</b>:
                      <label><input <?php if ($item['method'] == 'get'): ?>checked<?php endif;?> value="get" type="radio" name="param[<?php echo $k; ?>][method]">get</label>
                      <label><input <?php if ($item['method'] == 'post'): ?>checked<?php endif;?> value="post" type="radio" name="param[<?php echo $k; ?>][method]">post</label><br />
                      <a href="#" onclick="del_param(this)">删除</a>
                    </div>
                  <?php endif;?>
                <?php endforeach;?>
              <?php endif;?>

              <input type="button" name="add_param" id="add_param" value="添加参数" class="btn btn-lg btn-primary btn-block"><br />
              <input type="submit" name="submit" value="测试" class="btn btn-lg btn-primary btn-block"><br />
            </form>
          </div>
        </div>
        <div class="col-xs-1 col-lg-8">
          <?php
if (isset($_POST['submit'])) {
	echo "<pre style='white-space: pre-wrap;word-wrap: break-word;'>";
	echo "请求时间1:";
	var_dump($end - $start);

	echo "<br />请求url:";
	isset($url) && var_dump($url);

	echo "<br />请求参数:";
	isset($param) && var_dump($param);

	echo "<hr />结果：";
	if (isset($content['result'])) {
		echo "<br />code:";
		var_dump($content['result']['code']);
		echo "message:";
		var_dump($content['result']['message']);
		echo "data:";
		var_dump($content['result']['data']);
	} else {
		print_r($content);
	}

	echo "</pre>";
}
?>
      </div>
    </div>
  </div>
</div>
<hr />
</div>
<div class="blog-masthead">
  <div class="container">
    <nav class="blog-nav">
      <p class="blog-nav-item">&copy; beenlee 2016</p>
    </nav>
  </div>
</div>
</body>
</html>

<script>
  $("#add_param").click(function(){
    var input_len = $("form input").size();
    input_len++;
    $(this).before('\
      <div class="thumbnail">\
        <b>参数name</b>:\
        <input style="width: 200px;height: 25px;" value="" placeholder="请填写" type="text" name="param['+ input_len +'][name]"><br>\
        <b>参数value</b>:\
        <textarea style="width:330px;height: 200px;" value="" placeholder="请填写" type="text" name="param['+ input_len +'][value]"></textarea><br>\
        <b>请求方式</b>:\
        <label><input checked value="get" type="radio" name="param['+ input_len +'][method]">get</label>\
        <label><input value="post" type="radio" name="param['+ input_len +'][method]">post</label><br />\
        <a href="#" onclick="del_param(this)">删除</a>\
      </div>\
      ');
  });
  function del_param(obj) {
    $(obj).parent().remove();
  }
</script>
